package cn.wangzg.course1201;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BitmapActivity extends AppCompatActivity {
    static ByteArrayOutputStream byteOut = null;
    private Bitmap bitmap;
    double dScale=1.0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap);
    }

    public void btnCut(View view){
        //加载原始图片
        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.meinv);
        //
        Bitmap bitmap2 = Bitmap.createBitmap(bitmap1,0,0,200,200);
        ImageView img_bg = findViewById(R.id.imageView);
        img_bg.setImageBitmap(bitmap2);
    }
    //缩放功能
    public void btnScale(View view){
        //加载原始图片
        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.meinv);
        int width = bitmap1.getWidth();
        int height = bitmap1.getHeight();
        dScale+=0.1;
        Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap1,(int)(width*dScale),(int)(height*dScale),true);
        ImageView img_bg = findViewById(R.id.imageView);
        img_bg.setImageBitmap(bitmap2);
    }

    public void btnScreen(View view){
        captureScreen();
    }

    public void captureScreen() {
        Runnable action = new Runnable() {
            @Override
            public void run() {
                //获得当前的View 视图
                final View contentView = getWindow().getDecorView();
                try{
                    bitmap = Bitmap.createBitmap(contentView.getWidth(),
                            contentView.getHeight(), Bitmap.Config.ARGB_4444);
                    contentView.draw(new Canvas(bitmap));
                    byteOut = new ByteArrayOutputStream();
                    //保存
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteOut);
                    savePic(bitmap, "sdcard/short.png");
                }catch (Exception e){e.printStackTrace();}
                finally {
                    try{
                        if (null != byteOut)
                            byteOut.close();
                        if (null != bitmap && !bitmap.isRecycled()) {
//                            bitmap.recycle();
                            bitmap = null;
                        }
                    }catch (IOException e){e.printStackTrace();}

                }
            }
        };
        try {
            action.run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void savePic(Bitmap b, String strFileName) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(strFileName);
            if (null != fos) {
                boolean success= b.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.flush();
                fos.close();
                if(success)
                    Toast.makeText(BitmapActivity.this, "截屏成功", Toast.LENGTH_SHORT).show();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
